package com.shq.ex1;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import com.shq.ex1.bus.activity.*;

public class GjcActivity extends Activity {

    private Button btn01 = null;//站点查询
    private Button btn02 = null;//线路查询
    private Button btn03 = null;//换乘查询

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_gjc);

        btn01 = (Button) findViewById(R.id.btn01);
        btn02 = (Button) findViewById(R.id.btn02);
        btn03 = (Button) findViewById(R.id.btn03);


        final String path = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator  +"bus" ;
        final String databaseName = "zhengzhou.db";
        final String databasePath = path + File.separator +databaseName;
        if (!new File(path).exists()) {
            new File(path).mkdir();
        }
        if(!(new File(databasePath)).exists()){
            try {
                InputStream is = getResources().openRawResource(R.raw.zhengzhou);
                FileOutputStream fos = new FileOutputStream(databasePath);
                byte[] buffer = new byte[is.available()];
                int count = 0;
                while((count = is.read(buffer))>0){
                    fos.write(buffer, 0, count);
                }
                fos.close();
                is.close();
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }



        btn01.setOnClickListener(new OnClickListener() {//站点查询

            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.putExtra("databasePath", databasePath);
                intent.setClass(GjcActivity.this, ZhanActivity.class);
                GjcActivity.this.startActivity(intent);
            }
        });
        btn02.setOnClickListener(new OnClickListener() {//线路查询

            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.putExtra("databasePath", databasePath);
                intent.setClass(GjcActivity.this, XianActivity.class);
                GjcActivity.this.startActivity(intent);
            }
        });
        btn03.setOnClickListener(new OnClickListener() {//换乘查询

            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.putExtra("databasePath", databasePath);
                intent.setClass(GjcActivity.this, ZZhanActivity.class);
                GjcActivity.this.startActivity(intent);
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

